package org.example.managere.service.feibiao.impl;

import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmKnowledgeBasicDto;
import org.example.managere.entity.feibiao.AmKnowledgeBasic;
import org.example.managere.repository.feibiao.AmKnowledgeBasicRepository;
import org.example.managere.service.feibiao.AmKnowledgeBasicService;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 电机基础知识服务实现类 - 电机向量知识库
 */
@Service
@RequiredArgsConstructor
public class AmKnowledgeBasicServiceImpl implements AmKnowledgeBasicService {
    
    private final AmKnowledgeBasicRepository repository;
    private final ModelMapper modelMapper;
    
    /**
     * 将实体转换为DTO
     */
    private AmKnowledgeBasicDto convertToDto(AmKnowledgeBasic entity) {
        return modelMapper.map(entity, AmKnowledgeBasicDto.class);
    }
    
    /**
     * 将DTO转换为实体
     */
    private AmKnowledgeBasic convertToEntity(AmKnowledgeBasicDto dto) {
        return modelMapper.map(dto, AmKnowledgeBasic.class);
    }
    
    @Override
    @Transactional
    public AmKnowledgeBasicDto save(AmKnowledgeBasicDto dto) {
        AmKnowledgeBasic entity = convertToEntity(dto);
        entity.setInfoUpdateTime(new Date());
        AmKnowledgeBasic savedEntity = repository.save(entity);
        return convertToDto(savedEntity);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<AmKnowledgeBasicDto> findById(String motorId) {
        return repository.findById(motorId)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional
    public AmKnowledgeBasicDto update(AmKnowledgeBasicDto dto) {
        // 确保记录存在
        Optional<AmKnowledgeBasic> existingEntityOpt = repository.findById(dto.getMotorId());
        
        if (existingEntityOpt.isPresent()) {
            // 实体存在，更新
            AmKnowledgeBasic entity = convertToEntity(dto);
            entity.setInfoUpdateTime(new Date());
            AmKnowledgeBasic savedEntity = repository.save(entity);
            return convertToDto(savedEntity);
        } else {
            // 实体不存在，创建新的
            return save(dto);
        }
    }
    
    @Override
    @Transactional
    public void deleteById(String motorId) {
        repository.deleteById(motorId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeBasicDto> findAll() {
        return repository.findAll().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmKnowledgeBasicDto> findAll(Pageable pageable) {
        return repository.findAll(pageable)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmKnowledgeBasicDto> searchByKeyword(String keyword, Pageable pageable) {
        return repository.searchByKeyword(keyword, pageable)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeBasicDto> findByMotorType(String motorType) {
        return repository.findByMotorTypeContaining(motorType).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<AmKnowledgeBasicDto> findExactByMotorType(String motorType) {
        return Optional.ofNullable(repository.findByMotorType(motorType))
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeBasicDto> findByManufacturer(String manufacturer) {
        return repository.findByManufacturerContaining(manufacturer).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeBasicDto> findByRatedPowerRange(BigDecimal minPower, BigDecimal maxPower) {
        return repository.findByRatedPowerBetween(minPower, maxPower).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmKnowledgeBasicDto> findByRatedVoltageRange(BigDecimal minVoltage, BigDecimal maxVoltage) {
        return repository.findByRatedVoltageBetween(minVoltage, maxVoltage).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public long countByManufacturer(String manufacturer) {
        return repository.countByManufacturer(manufacturer);
    }
} 